﻿using System;
using System.Collections.Generic;
using System.Linq;
using AMM.CustomClassLibrary;
using AMM.DataAccess.Store;
using AMM.Model;
using AMM.Model.Store;

namespace AMM.Business.Store {
    public class InStoreOrderProcessor : BaseProcessor {

        private int _pageSize;
        private InStoreOrderRepository _inStoreOrderRepository;

        public InStoreOrderProcessor(int pageSize = int.MaxValue) {
            this._pageSize = pageSize;
            this._inStoreOrderRepository = new InStoreOrderRepository(pageSize);
        }

        public InStoreOrder FetchInStoreOrderById(int inStoreOrderId) {
            try {
                return this._inStoreOrderRepository.FetchInStoreOrderById(inStoreOrderId);
            }
            catch (Exception ex) {
                Logger.Error(ex.Message, ex);
                throw this.BuildException("获取指定入库单失败！");
            }
        }

        public List<InStoreOrderDetailsViewModel> FetchInStoreOrderDetailsViewModelList(int inStoreOrderId) {
            try {
                return this._inStoreOrderRepository.FetchInStoreOrderByIdForView(inStoreOrderId).InStoreOrderDetails.Select(x => {
                    return new InStoreOrderDetailsViewModel() {
                        Key = x.InStoreOrderDetailsId,
                        MaterialCode = x.Material.Code,
                        Quantity = x.Quantity,
                        CreatedBy = x.CreatedBy,
                        CreatedDatetime = x.CreatedDatetime
                        //,ModifiedBy = x.ModifiedBy,
                        //ModifiedDatetime = x.ModifiedDatetime
                    };
                }).ToList();
            }
            catch (Exception ex) {
                Logger.Error(ex.Message, ex);
                throw this.BuildException("获取指定入库单失败！");
            }
        }

        public List<InStoreOrderViewModel> FetchInStoreOrderViewModelList(InStoreOrderSearchModel searchModel, int pageIndex = 1) {
            try {
                List<InStoreOrder> resultList = this._inStoreOrderRepository.FetchInStoreOrderList(searchModel, pageIndex);
                return resultList.Select(x => {
                    return new InStoreOrderViewModel() {
                        Key = x.InStoreOrderId,
                        Code = x.Code,
                        InStoreType = x.Basic.Code,
                        StoreBasisCode = x.StoreBasi.Code,
                        SuspenseOrderCode = x.SuspenseOrder.Code,
                        Remarks = x.Remarks,
                        CreatedBy = x.CreatedBy,
                        CreatedDatetime = x.CreatedDatetime
                        //,ModifiedBy = x.ModifiedBy,
                        //ModifiedDatetime = x.ModifiedDatetime
                    };
                }).ToList();
            }
            catch (Exception ex) {
                Logger.Error(ex.Message, ex);
                throw this.BuildException("获取入库单列表失败！");
            }
        }

        public void Insert(InStoreOrder inStoreOrder) {
            try {
                inStoreOrder.InStoreOrderId = Guid.NewGuid().GetHashCode();
                inStoreOrder.CreatedDatetime = DateTime.Now;
                inStoreOrder.InStoreOrderDetails.ToList().ForEach(x => {
                    x.CreatedBy = inStoreOrder.CreatedBy;
                    x.CreatedDatetime = inStoreOrder.CreatedDatetime;
                    x.InStoreOrderDetailsId = Guid.NewGuid().GetHashCode();
                    x.InStoreOrderId = inStoreOrder.InStoreOrderId;
                });
                this._inStoreOrderRepository.InsertInStoreOrder(inStoreOrder);
            }
            catch (Exception ex) {
                Logger.Error(ex.Message, ex);
                throw this.BuildException("插入新入库单失败！");
            }
        }

        public void Update(InStoreOrder inStoreOrder) {
            try {
                InStoreOrder oldInStoreOrder = this.FetchInStoreOrderById(inStoreOrder.InStoreOrderId);
                if (!oldInStoreOrder.RowVersion.SequenceEqual(inStoreOrder.RowVersion)) {
                    throw this.BuildException("更新入库单失败，数据不一致性错误！");
                }
                inStoreOrder.ModifiedDatetime = DateTime.Now;
                this._inStoreOrderRepository.UpdateInStoreOrder(inStoreOrder);
            }
            catch (Exception ex) {
                Logger.Error(ex.Message, ex);
                throw this.BuildException("更新入库单失败！");
            }
        }

        public void Delete(InStoreOrder inStoreOrder) {
            try {
                this._inStoreOrderRepository.Delete(inStoreOrder);
            }
            catch (Exception ex) {
                Logger.Error(ex.Message, ex);
                throw this.BuildException("删除入库单失败！");
            }
        }

        public void Delete(params int[] inStoreOrderIds) {
            try {
                this._inStoreOrderRepository.DeleteMultipleInStoreOrder(inStoreOrderIds);
            }
            catch (Exception ex) {
                Logger.Error(ex.Message, ex);
                throw this.BuildException("删除入库单失败！");
            }
        }

        public int GetTheMaxFlowNumber(string startString) {
            try {
                return this._inStoreOrderRepository.GetTheMaxFlowNumber(startString) + 1;
            }
            catch (Exception ex) {
                Logger.Error(ex.Message, ex);
                throw this.BuildException("获取入库单最大流水号失败！");
            }
        }

        public List<InStoreOrderDetail> FetchInStoreOrderDetailList(int suspenseOrderId) {
            try {
                return this._inStoreOrderRepository.FetchInStoreOrderDetailList(suspenseOrderId);
            }
            catch (Exception ex) {
                Logger.Error(ex.Message, ex);
                throw this.BuildException("获取入库单明细列表失败！");
            }
        }
    }
}
